package com.ynet.middleground.mobile.approve.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.mobile.approve.commom.ResponseResult;
import com.ynet.middleground.mobile.approve.request.SendLendersCaptchaCodeReq;
import com.ynet.middleground.mobile.approve.service.MessageService;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author liulx
 * @description 消息推送服务
 * @date 2020-12-15 22:36
 */
@RestController
@RequestMapping("message")
@ApiIgnore
public class MessageController {

    @Autowired
    MessageService messageService;

    /**
     * 注意：该方法仅供测试
     * 
     * @param type
     * @param userId
     * @return
     */
    @RequestMapping("send")
    public ServiceResult<Long> sendWorkingMessage(@RequestParam String type, @RequestParam String userId) {
        String message = messageService.getMessageTest(type, "测试内容");
        return messageService.sendWorkingMessage(userId, message);
    }

    /**
     * 推送钉钉通知 <br>
     * 
     * @param req
     * @return
     */
    @PostMapping("/send-dingtalk")
    @ApiOperation(value = "推送钉钉通知消息，审批中心调用，也可用作手动通知补偿备用服务", notes = "推送钉钉通知消息，审批中心调用，也可用作手动通知补偿备用服务")
    public ResponseResult<Object>
        sendDingtalkNotificationAfterApproval(@RequestBody @Valid SendLendersCaptchaCodeReq req) {
        ResponseResult<Object> responseResult = new ResponseResult<>();
        try {
            messageService.sendDingtalkNotificationAfterApproval(req);
        } catch (BusinessException e) {
            responseResult.setError(e.getCode(), e.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "推送钉钉通知消息服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            responseResult.setError("ECMA0000", "服务异常，请联系系统管理员");
        }
        return responseResult;
    }

}
